﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fcom.VNInsurance.Web.Models;

namespace Fcom.VNInsurance.Web.Controllers
{
     [Authorize(Roles = "13,14")]
    public class AutoCategoryManagerController : Controller
    {
        //
        // GET: /AutoCategoryManager/

        Fcom.VNInsurance.Web.Models.VNInsuranceDataContext db = new Models.VNInsuranceDataContext();
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult AddAutoCategory()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddAutoCategory(AutoCategory com)
        {
            AutoCategory cpn = new AutoCategory();
            var isexit = db.AutoCategories.Where(m => m.Name == com.Name).FirstOrDefault();
            if (isexit == null)
            {
                cpn.Name = com.Name;
                db.AutoCategories.InsertOnSubmit(cpn);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
        public JsonResult listautocat()
        {
            var autocatlist = (from a in db.AutoCategories
                               select new
                               {
                                   ComID = a.ID,
                                   Name = a.Name,
                                  
                               });
            return Json(autocatlist, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public ActionResult EditAutoCat(int id)
        {
            var company = db.AutoCategories.Where(m => m.ID == id).FirstOrDefault();
            try
            {
                return View(company);

            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
        [HttpPost]
        public ActionResult EditAutoCat(AutoCategory autocat)
        {
            var foud = db.AutoCategories.Where(m => m.ID == autocat.ID).FirstOrDefault();
            if (foud != null)
            {
                foud.Name = autocat.Name;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        [HttpPost]
        public ActionResult removeauto(int id)
        {
            var at = db.AutoCategories.Where(m => m.ID == id).FirstOrDefault();
            db.Cars.DeleteAllOnSubmit(at.Cars);
            db.Motobikes.DeleteAllOnSubmit(at.Motobikes);
            db.AutoCategories.DeleteOnSubmit(at);
            db.SubmitChanges();
            return Json(true);
        }


    }
}
